• File: ajax_create_job.php
  • Full Path: C:/htdocs/reeft_gps_test/REEFTintegrationLog/reeft_gps/ajax_create_job.php
  • Date Modified: 05/07/2025 3:27 PM
  • File size: 5.71 KB
  • MIME-type: text/x-php
  • Charset: utf-8
<?php
//======================================================================================
//
// Function: Create new job
//
// Programmer: AR
// Date      : 2025-05-06
//
// Copyright Reeft A/S (c) - 2025
//======================================================================================

//======================================================================================	
// Set session
//======================================================================================			
if(!isset($_SESSION))
{ 
	session_start();
}

//======================================================================================
// General 
//======================================================================================
include "include/apikey.php";
include "rft_apicall.php";
include "REEFT_date_convert/REEFT_date_convert.php";
$returnResult = [];
$returnResult["error"] = "";

//======================================================================================
// Get input
//======================================================================================
if (isset($_SESSION["receivedToken"]))	{
	$receivedToken = $_SESSION["receivedToken"];
} else {
	$returnResult["error"]	.= "receivedToken missing";
	echo json_encode($returnResult);
	exit;
}
if (isset($_SESSION["ConsiderOrganizationHeader"]))	{
	$ConsiderOrganizationHeader = $_SESSION["ConsiderOrganizationHeader"];
} else {
	$ConsiderOrganizationHeader	= "";
}
if (isset($_SESSION["UseSharedDb"]))	{
	$UseSharedDb = $_SESSION["UseSharedDb"];
} else {
	$UseSharedDb				= "";
}
if (isset($_SESSION["TargetOrganization"]))	{
	$TargetOrganization = $_SESSION["TargetOrganization"];
} else {
	$TargetOrganization			= "";
}

if (isset($_REQUEST["accountId"]))	{
	$accountId = $_REQUEST["accountId"];
} else {
	$returnResult["error"]	.= "accountId missing";
	echo json_encode($returnResult);
	exit;
}

if (isset($_REQUEST["departmentId"]))	{
	$departmentId = $_REQUEST["departmentId"];
} else {
	$returnResult["error"]	.= "departmentId missing";
	echo json_encode($returnResult);
	exit;
}

if (isset($_REQUEST["activityTypeId"]))	{
	$activityTypeId = $_REQUEST["activityTypeId"];
} else {
	$returnResult["error"]	.= "activityTypeId missing";
	echo json_encode($returnResult);
	exit;
}

if (isset($_REQUEST["shortDescription"]))	{
	$shortDescription = $_REQUEST["shortDescription"];
} else {
	$returnResult["error"]	.= "shortDescription missing";
	echo json_encode($returnResult);
	exit;
}

if (isset($_REQUEST["startDate"]))	{
	$startDate = $_REQUEST["startDate"];
} else {
	$returnResult["error"]	.= "startDate missing";
	echo json_encode($returnResult);
	exit;
}

if (isset($_REQUEST["fromTime"]))	{
	$fromTime = $_REQUEST["fromTime"];
} else {
	$returnResult["error"]	.= "fromTime missing";
	echo json_encode($returnResult);
	exit;
}

if (isset($_REQUEST["toTime"]))	{
	$toTime = $_REQUEST["toTime"];
} else {
	$returnResult["error"]	.= "toTime missing";
	echo json_encode($returnResult);
	exit;
}

if (isset($_REQUEST["selectedEmp"]))	{
	$selectedEmp = $_REQUEST["selectedEmp"];
} else {
	$selectedEmp = $_REQUEST["selectedEmp"];
}

if (isset($_REQUEST["selectedServiceUnit"]))	{
	$selectedServiceUnit = $_REQUEST["selectedServiceUnit"];
} else {
	$selectedServiceUnit = $_REQUEST["selectedServiceUnit"];
}

if (isset($_REQUEST["longDescription"]))	{
	$longDescription = $_REQUEST["longDescription"];
} else {
	$longDescription = $_REQUEST["longDescription"];
}

$startDatetime = $startDate . "T" . $fromTime;
$dueDatetime = $startDate . "T" . $toTime;

$startDatetimeUTC = REEFT_local_to_utc($startDatetime);
$dueDatetimeUTC = REEFT_local_to_utc($dueDatetime);

$createdAt = date("Y-m-d\TH:i:s"); 

$time1 = new DateTime($fromTime);
$time2 = new DateTime($toTime);
$interval = $time1->diff($time2);

$estimatedHours = $interval->h + ($interval->i / 60);


$headers = [
		'accept: text/plain',
		'Content-Type: application/json',
		'ConsiderOrganizationHeader: ' . $ConsiderOrganizationHeader,
		'UseSharedDb: ' . $UseSharedDb,
		'TargetOrganization: ' . $TargetOrganization,
	];
	
$job = [
	'accountId' => $accountId,
	'departmentId' => $departmentId,
	'activityTypeId' => $activityTypeId,
	'shortDescription' => $shortDescription,
	'startDatetime' => $startDatetimeUTC,
	'dueDatetime' => $dueDatetimeUTC,
	'estimatedHours' => $estimatedHours,
	'createdAt' => $createdAt, 
	'createdBy' => $_SESSION["loginUserId"]
];

if ($longDescription != "") $job['longDescription'] = $longDescription;
if ($selectedServiceUnit != "") $job['serviceUnitId'] = $selectedServiceUnit;
	
$data = [	
		"OrganizationId" => $TargetOrganization,
		"job" => $job ,
	];


$url = $rftUrl . '/Job/Create';

$response = makeApiCall($url, $headers, $data, 'POST');

if (isset($response['error'])) {
    $returnResult["error"]	.= $response['error'];
} else if (isset($response['data'])) {
	// Add assignment and schedule
	$jobId = $response['data'];
	$returnResult["jobId"] = $jobId;
	
	if ($selectedEmp != "") {
		
		$data = [	
			"userIds" => [$selectedEmp],
			"jobId" => $jobId,
		];

		$url = $rftUrl . '/JobAssignment/Add';
		
		$responseAssignment = makeApiCall($url, $headers, $data, 'POST');
		
		$jobSchedules = [	
			"startDateTime" => $startDatetimeUTC,
			"endDateTime" => $dueDatetimeUTC ,
		];
		
		$data = [	
			"users" => [$selectedEmp],
			"jobId" => $jobId,
			"organizationId" => $TargetOrganization,
			"jobSchedules" => [$jobSchedules],
		];

		$url = $rftUrl . '/JobSchedule/Add';
		
		$responseSchedule = makeApiCall($url, $headers, $data, 'POST');
	} 
	
} else {
	$returnResult["error"]	.= "No job ID returned";
}

echo json_encode($returnResult);